#include "bird.h"
#include "ui_bird.h"
#include<QPainter>
#include<QTimer>
#include<QKeyEvent>
bird::bird(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::bird),blsrunbir(false)
{
    ui->setupUi(this);
    this->setGeometry(QRect(600,300,460,400));//页面位置及大小
}
bird::~bird()
{
    delete ui;
}

void bird::paintEvent(QPaintEvent *event){
     QPainter painters(this);
     if(!blsrunbir){
         initbir();
     initwall();
     }
     //游戏背景
     painters.setPen(Qt::gray);//总
     painters.setBrush(Qt::gray);
     painters.drawRect(0,0,1000000,100000);
     painters.setPen(Qt::black);//外
     painters.setBrush(Qt::blue);
     painters.drawRect(10,10,450,270);
     painters.setPen(Qt::black);//内
     painters.setBrush(Qt::black);
     painters.drawRect(25,25,420,240);
     //分格
     /*painters.setPen(Qt::white);
     for(int i = 25;i <= 265;i+=15){//横
         painters.drawLine(25,i,445,i);
     }
     for(int i = 25;i <= 445;i+=15){//竖
         painters.drawLine(i,25,i,265);
     }*/
     //画墙
     for(int i = 0;i < 100;i++){
     painters.setPen(Qt::gray);
     painters.setBrush(Qt::gray);
     painters.drawRect(rectw[i]);
     }
     //显示游戏开始与结束
     QFont font1("courier",24);//24号字体
     painters.setFont(font1);
     painters.setPen(Qt::red);
     painters.setBrush(Qt::red);
     painters.drawText(100,350,display);//显示位置
     //画鸟
     painters.setPen(Qt::red);
     painters.setBrush(Qt::red);
     painters.drawRect(vbirrect);
     if(blsover){//游戏结束
         display="别灰心";
         timer->stop();
         timer1->stop();

     }
}
void bird::initwall(){//所有墙初始化
       rectw.resize(100);
    for(int i = 0;i < 100;i++){
        QRect rectx(415+i*60,(qrand()%240+25),60,15);
        rectw[i]=rectx;
    }
    //一个墙的初始位置(415,25,60,15)
}
void bird::initbir(){
    display="";//开始显示内容
    blsrunbir=true;
    blsover=false;
    QRect rectb(175,115,15,15);//鸟初始位置及大小
    vbirrect=rectb;
    timer=new QTimer();//设计计时器
    timer->start(100);//每100ms触发一次：鸟的移动速度
    timer1=new QTimer();
    timer1->start(30);//墙和鸟下落速度调节
    connect(timer,SIGNAL(timeout()),SLOT(bir_update()));
    connect(timer1,SIGNAL(timeout()),SLOT(bir_down()));
}
void bird::wall_update(){
    vwallrect.setLeft(vwallrect.left()-10);
    vwallrect.setRight(vwallrect.right()-10);
    update();
}
void bird::bir_update(){
    display="针不戳,玩得针不戳";//游戏进行中
    if(ndierc_bir==1){//鸟向上蹦
        vbirrect.setTop(vbirrect.top()-10);
        vbirrect.setBottom(vbirrect.bottom()-10);
        ndierc_bir=0;
    }
    if(vbirrect.top()<24){//游戏结束判断
        display="撞墙了呜呜，别灰心";
        blsover=true;
    }
    update();//更新
}
void bird::bir_down(){//鸟下落墙移动
    vbirrect.setTop(vbirrect.top()+1);
    vbirrect.setBottom(vbirrect.bottom()+1);
    for(int i = 0;i  < 100;i++){
    rectw[i].setLeft(rectw[i].left()-2);
    rectw[i].setRight(rectw[i].right()-2);
    }
    if(vbirrect.bottom()>256){//游戏结束判断
        display="撞墙了呜呜，别灰心";
        blsover=true;
    }
    for(int i = 0;i < 100;i++){//判断是否撞墙
        if(rectw[i].left()<=175&&rectw[i].right()>=180){
            if(vbirrect.bottom()>=rectw[i].top()&&vbirrect.bottom()<rectw[i].bottom()){//鸟下界碰撞
                display="撞墙了呜呜，别灰心";
                blsover=true;
            }
            else if(vbirrect.top()<=rectw[i].bottom()&&vbirrect.top()>rectw[i].top()){//鸟上界碰撞
                display="撞墙了呜呜，别灰心";
                blsover=true;
            }
           }

    }
   update();
}
void bird::keyPressEvent(QKeyEvent *event){//键盘事件
    QKeyEvent *key=(QKeyEvent*) event;
    if(key->key()==Qt::Key_Space){
        ndierc_bir=1;
    }
}


























